package one.stand.service.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import one.stand.enums.SeeRecordEnums;
import one.stand.mapper.SeeRecordPublicMapper;
import one.stand.model.ResultModel;
import one.stand.request.SeeRecordRequest;
import one.stand.response.CrmCustomerAnalysisRespnse;
import one.stand.service.RecordPublicService;
import one.stand.service.SeeRecordService;
import one.stand.util.OneConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Service
@Slf4j
public class SeeRecordServiceImpl implements SeeRecordService {
    @Autowired
    private RecordPublicService recordPublicService;
    @Autowired
    private SeeRecordPublicMapper seeRecordPublicMapper;

    @Override
    public ResultModel<List<CrmCustomerAnalysisRespnse>> countCustomerAnalysis(SeeRecordRequest recordRequest) {
        log.info("圆饼图：{}", JSON.toJSONString(recordRequest));
        if (recordRequest == null) {
            return ResultModel.checkParamFail();
        }
        Integer userId = null;
        Integer cardId = null;
        Integer companyId = recordRequest.getCId().intValue();
        Integer dayNum = recordRequest.getDayNum();
        if (recordRequest.getCardId() == null) {
            cardId = recordRequest.getMyCardId();
        } else if (recordRequest.getType() != null && recordRequest.getType() == 1) {
            cardId = recordRequest.getCardId();
        }
        if (recordRequest.getUserId() != null) {
            userId = recordRequest.getUserId().intValue();
        }
        List<CrmCustomerAnalysisRespnse> list = seeRecordPublicMapper.countCustomerAnalysis(userId, cardId, companyId, dayNum);
        if (list == null || list.size() <= 0) {
            return ResultModel.noData();
        }
        return ResultModel.success(list);
    }

    @Override
    public ResultModel<Map<String, Object>> getPcCount(SeeRecordRequest request) {
        log.info("CRM-PC端首页统计:{}", JSON.toJSONString(request));
        if (request == null) {
            return ResultModel.checkParamFail();
        }
        Integer companyId = request.getCId().intValue();
        Integer cardId = request.getCardId();
        if (OneConstant.ONE.equals(request.getType())) {
            cardId = request.getMyCardId();
        }
        Map<String, Object> map = seeRecordPublicMapper.getPcCount(null, cardId, companyId);
        if (map == null) {
            return ResultModel.noData();
        }
        // 查询未跟进客户头像-最多5张
        map.put("photos", seeRecordPublicMapper.getPcPhotos(cardId, companyId));
        // 查询今日录入客户列表
        map.put("newCustomerList", seeRecordPublicMapper.selectPcIndexCustomer(cardId, companyId));
        return ResultModel.success(map);
    }

    @Override
    public ResultModel<Map<String, Object>> getChangeCount(SeeRecordRequest request) {
        log.info("CRM-PC端首页转化率漏斗:{}", JSON.toJSONString(request));
        if (request == null) {
            return ResultModel.checkParamFail();
        }
        Integer companyId = request.getCId().intValue();
        Integer cardId = OneConstant.ONE.equals(request.getType()) ? request.getMyCardId() : request.getCardId();
        Integer dayNum = request.getDayNum() == null ? 30 : request.getDayNum();
        Integer seeType = SeeRecordEnums.PRODUCT.getCode();
        Map<String, Object> list = seeRecordPublicMapper.getChangeCount(cardId, companyId, dayNum);
        if (list == null || list.size() <= 0) {
            return ResultModel.noData();
        }
        list.put("seeGoodsNum", seeRecordPublicMapper.getChangeCountOne(cardId, companyId, dayNum, seeType));
        return ResultModel.success(list);
    }
}
